package com.bj58.ecdata.dashboard.databll;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.TreeMap;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bj58.ecdata.dashboard.cache.ServiceBeanFactory;
import com.bj58.ecdata.dashboard.constant.IncomeCateConst;
import com.bj58.ecdata.dashboard.constant.IncomeCityConst;
import com.bj58.ecdata.dashboard.constant.IncomePlatConst;
import com.bj58.ecdata.dashboard.dbentity.IncomeCate;
import com.bj58.ecdata.dashboard.dbentity.IncomeCateConsume;
import com.bj58.ecdata.dashboard.dbentity.IncomeCateRadar;
import com.bj58.ecdata.dashboard.dbentity.IncomeCity;
import com.bj58.ecdata.dashboard.dbentity.IncomeCityConsume;
import com.bj58.ecdata.dashboard.dbentity.IncomeCityRadar;
import com.bj58.ecdata.dashboard.dbentity.IncomePlatform;
import com.bj58.ecdata.dashboard.service.AbstractBaseService;
import com.bj58.ecdata.dashboard.utils.DNumberUtils;
import com.bj58.ecdata.dashboard.utils.ReflectUtil;
import com.google.common.collect.Lists;

public class IncomeBLL {
	private static AbstractBaseService<IncomeCity> icmCityService = ServiceBeanFactory.get(IncomeCity.class,null);
	private static AbstractBaseService<IncomeCityRadar> icmCityRadarService = ServiceBeanFactory.get(IncomeCityRadar.class,null);
	private static AbstractBaseService<IncomeCateRadar> icmCateRadarService = ServiceBeanFactory.get(IncomeCateRadar.class,null);
	private static AbstractBaseService<IncomeCate> icmCateService = ServiceBeanFactory.get(IncomeCate.class,null);
	private static AbstractBaseService<IncomeCityConsume> icmCityConsumeService = ServiceBeanFactory.get(IncomeCityConsume.class,null);
	private static AbstractBaseService<IncomeCateConsume> icmCateConsumeService = ServiceBeanFactory.get(IncomeCateConsume.class,null);
	private static AbstractBaseService<IncomePlatform> icmPlatService = ServiceBeanFactory.get(IncomePlatform.class,null);
	
	/**
	 * 城市对应的产品(精准，置顶，智能)趋势图数据
	 */
	public static String getCityProJson(String dateGroup, String stat_type, String businessName, String cityName){
		String fields[] ={"jingzhun_cash","jingzhun_user","zhiding_cash","zhiding_user","zhineng_cash","zhineng_user"};
		String cols = IncomeCityConst.getCityProCol(dateGroup);
		String condition =  IncomeCityConst.getCityProCon(dateGroup, stat_type, businessName, cityName);
		List<IncomeCity> list = icmCityService.getListByConditon( cols, condition, dateGroup);
		TreeMap<String, List<IncomeCity>> stat_dateMap = new TreeMap<String, List<IncomeCity>>(ReflectUtil.groupByField(list,dateGroup));

		JSONObject ob = new JSONObject();
		HashMap<String, List<Number>> dataMap = new HashMap<String, List<Number>>(); // stat_date : [dataList]
		for (String date : stat_dateMap.keySet()) {
			IncomeCity cityVo = stat_dateMap.get(date).get(0);
			for (String f : fields) {
				List<Number> curList = dataMap.get(f);
				if(curList == null)
					curList = new ArrayList<Number>();
				curList.add((Number) ReflectUtil.getVal(cityVo, f));
				dataMap.put(f, curList);
			}
		}
		ob.put("time", stat_dateMap.keySet());
		
		for(String p:dataMap.keySet()){
			ob.put(p, dataMap.get(p));
		}

		return ob.toString();
	}
		
	/**
	 * echarts点击获取城市对应的产品(精准，置顶，智能) 表格展示数据
	 */
	public static String getCityProTableJson(String dateGroup, String stat_type, String businessName, String dateVal){
		String condition =  IncomeCityConst.getCityProTableCon(dateGroup, stat_type, businessName, dateVal);
		List<IncomeCity> list = icmCityService.getListByConditon( null, condition, "jingzhun_cash desc");

		JSONObject ob = new JSONObject();
		ob.put("dataList", list);

		return ob.toString();
	}

	/**
	 * 产品(精准|置顶|智能)的城市对比趋势图数据
	 */
	public static String getCityCmpJson(String dateGroup, String stat_type, String businessName,String proType, String city1, String city2){
		String[] citys={city1,city2};
		String fields[] ={proType+"_cash",proType+"_user"};
		String cols = IncomeCityConst.getCityCmpCol(dateGroup, proType);
		String condition =  IncomeCityConst.getCityCmpCon(dateGroup, stat_type, businessName,city1,city2);
		List<IncomeCity> list = icmCityService.getListByConditon( cols, condition, null);
		TreeMap<String, List<IncomeCity>> stat_dateMap = new TreeMap<String, List<IncomeCity>>(ReflectUtil.groupByField(list,dateGroup));

		HashMap<String, List<Number>> dataMap = new HashMap<String, List<Number>>(); // key : [dataList]
		for (String date : stat_dateMap.keySet()) {
			for (String ct : citys) {
				IncomeCity cityVo = ReflectUtil.getByColVal(IncomeCity.class,stat_dateMap.get(date),"city_name",ct);
				for (String f : fields) {
					String key = ct+"_"+f;
					List<Number> curList = dataMap.get(key);
					if(curList == null)
						curList = new ArrayList<Number>();
					curList.add((Number) ReflectUtil.getVal(cityVo, f));
					dataMap.put(key, curList);
				}
			}
		}

		JSONObject ob = new JSONObject();
		ob.put("time", stat_dateMap.keySet());
		
		for(String p:dataMap.keySet()){
			ob.put(p, dataMap.get(p));
		}

		return ob.toString();
	}
	
	/**
	 * 城市消耗周期
	 */
	public static String getCityConsumeJson(String dateGroup,String statType, 
			String businessName, String consume_citys, String index1, String index2) {
		
		HashMap<String, List<Number>> dataMap = new HashMap<String, List<Number>>(); // key : [dataList]
		String[] indexs  = {index1,index2};
		List<String> cityArr = Lists.newArrayList(consume_citys.split(","));
		
		String cols = IncomeCityConst.getCityConsumeCol(dateGroup,index1,index2);
		String condition =  IncomeCityConst.getCityConsumeCon(businessName,dateGroup,statType, cityArr);
		List<IncomeCityConsume> list = icmCityConsumeService.getListByConditon( cols, condition, null);
		TreeMap<String, List<IncomeCityConsume>> stat_dateMap = new TreeMap<String, List<IncomeCityConsume>>(ReflectUtil.groupByField(list,dateGroup));
		for (String date : stat_dateMap.keySet()) {
			for (Object ct : cityArr) {
				IncomeCityConsume cityVo = ReflectUtil.getByColVal(IncomeCityConsume.class,stat_dateMap.get(date),"city_name",ct);
				for (String f : indexs) {
					String key = ct+"_"+f;
					List<Number> curList = dataMap.get(key);
					if(curList == null)
						curList = new ArrayList<Number>();
					curList.add((Number) ReflectUtil.getVal(cityVo, f));
					dataMap.put(key, curList);
				}
			}
		}

		JSONObject ob = new JSONObject();
		ob.put("time", stat_dateMap.keySet());
		
		for(String p:dataMap.keySet()){
			ob.put(p, dataMap.get(p));
		}

		return ob.toString();
	}
	
	
	/**
	 * echarts点击获取  城市消耗周期 表格展示数据
	 */
	public static String getCityConsumeTableJson(String dateGroup,String statType,  String businessName, String dateVal){
		String condition =  IncomeCityConst.getCityConsumeTableCon(businessName,dateGroup,statType, dateVal);
		List<IncomeCityConsume> list = icmCityConsumeService.getListByConditon( null, condition, "balance desc");

		JSONObject ob = new JSONObject();
		ob.put("dataList", list);

		return ob.toString();
	}
	
	
	/**
	 * 类别消耗周期
	 */
	public static String getCateConsumeJson(String dateGroup, String statType, 
			String businessName, String consume_cates, String index1, String index2) {
		
		HashMap<String, List<Number>> dataMap = new HashMap<String, List<Number>>(); // key : [dataList]
		String[] indexs  = {index1,index2};
		List<String> cateArr = Lists.newArrayList(consume_cates.split(","));
		
		String cols = IncomeCateConst.getCateConsumeCol(dateGroup,index1,index2);
		String condition =  IncomeCateConst.getCateConsumeCon( businessName,dateGroup, statType, cateArr);
		List<IncomeCateConsume> list = icmCateConsumeService.getListByConditon( cols, condition, null);
		TreeMap<String, List<IncomeCateConsume>> stat_dateMap = new TreeMap<String, List<IncomeCateConsume>>(ReflectUtil.groupByField(list,dateGroup));
		for (String date : stat_dateMap.keySet()) {
			for (Object ct : cateArr) {
				IncomeCateConsume cityVo = ReflectUtil.getByColVal(IncomeCateConsume.class,stat_dateMap.get(date),"cate_name",ct);
				for (String f : indexs) {
					String key = ct+"_"+f;
					List<Number> curList = dataMap.get(key);
					if(curList == null)
						curList = new ArrayList<Number>();
					curList.add((Number) ReflectUtil.getVal(cityVo, f));
					dataMap.put(key, curList);
				}
			}
		}

		JSONObject ob = new JSONObject();
		ob.put("time", stat_dateMap.keySet());
		for(String p:dataMap.keySet()){
			ob.put(p, dataMap.get(p));
		}

		return ob.toString();
	}
	
	
	/**
	 * echarts点击获取  城市消耗周期 表格展示数据
	 */
	public static String getCateConsumeTableJson(String dateGroup, String statType, String businessName, String dateVal){
		String condition =  IncomeCateConst.getCateConsumeTableCon(businessName,dateGroup, statType, dateVal);
		List<IncomeCateConsume> list = icmCateConsumeService.getListByConditon( null, condition, "balance desc");

		JSONObject ob = new JSONObject();
		ob.put("dataList", list);

		return ob.toString();
	}
	
	/**
	 * 类别  对应的产品(精准，置顶，智能)趋势图数据
	 */
	public static String getCateProJson(String dateGroup, String stat_type, String businessName, String cateName){
		String fields[] ={"jingzhun_cash","jingzhun_user","zhiding_cash","zhiding_user","zhineng_cash","zhineng_user"};
		String cols = IncomeCateConst.getCateProCol(dateGroup);
		String condition =  IncomeCateConst.getCateProCon(dateGroup, stat_type, businessName, cateName);
		List<IncomeCate> list = icmCateService.getListByConditon( cols, condition, dateGroup);
		TreeMap<String, List<IncomeCate>> stat_dateMap = new TreeMap<String, List<IncomeCate>>(ReflectUtil.groupByField(list,dateGroup));

		JSONObject ob = new JSONObject();
		HashMap<String, List<Number>> dataMap = new HashMap<String, List<Number>>(); // stat_date : [dataList]
		for (String date : stat_dateMap.keySet()) {
			IncomeCate cateVo = stat_dateMap.get(date).get(0);
			for (String f : fields) {
				List<Number> curList = dataMap.get(f);
				if(curList == null)
					curList = new ArrayList<Number>();
				curList.add((Number) ReflectUtil.getVal(cateVo, f));
				dataMap.put(f, curList);
			}
		}
		ob.put("time", stat_dateMap.keySet());

		for(String p:dataMap.keySet()){
			ob.put(p, dataMap.get(p));
		}

		return ob.toString();
	}
		
	/**
	 * echarts点击获取 类别  对应的产品(精准，置顶，智能) 表格展示数据
	 */
	public static String getCateProTableJson(String dateGroup, String stat_type, String businessName, String dateVal){
		String condition =  IncomeCateConst.getCateProTableCon(dateGroup, stat_type, businessName, dateVal);
		List<IncomeCate> list = icmCateService.getListByConditon( null, condition, "jingzhun_cash desc");
		JSONObject ob = new JSONObject();
		ob.put("dataList", list);
		return ob.toString();
	}
	
	/**
	 * 产品(精准|置顶|智能)的 类别 对比趋势图数据
	 */
	public static String getCateCmpJson(String dateGroup, String stat_type, String businessName,String proType, String cate1, String cate2){
		String[] cates={cate1,cate2};
		String fields[] ={proType+"_cash",proType+"_user"};
		String cols = IncomeCateConst.getCateCmpCol(dateGroup, proType);
		String condition =  IncomeCateConst.getCateCmpCon(dateGroup, stat_type, businessName,cate1,cate2);
		List<IncomeCate> list = icmCateService.getListByConditon( cols, condition, null);
		TreeMap<String, List<IncomeCate>> stat_dateMap = new TreeMap<String, List<IncomeCate>>(ReflectUtil.groupByField(list,dateGroup));

		HashMap<String, List<Number>> dataMap = new HashMap<String, List<Number>>(); // key : [dataList]
		for (String date : stat_dateMap.keySet()) {
			for (String ct : cates) {
				IncomeCate cateVo = ReflectUtil.getByColVal(IncomeCate.class,stat_dateMap.get(date),"cate_name",ct);
				for (String f : fields) {
					String key = ct+"_"+f;
					List<Number> curList = dataMap.get(key);
					if(curList == null)
						curList = new ArrayList<Number>();
					curList.add((Number) ReflectUtil.getVal(cateVo, f));
					dataMap.put(key, curList);
				}
			}
		}

		JSONObject ob = new JSONObject();
		ob.put("time", stat_dateMap.keySet());
		
		for(String p:dataMap.keySet()){
			ob.put(p, dataMap.get(p));
		}

		return ob.toString();
	}

	/**
	 * 城市雷达图
	 */
	public static String getCityRadarJson(String stat_type,String dateVal,
			String businessName, String citys_radar,String is_vip) {
		if(StringUtils.equals(stat_type, "week")){
			dateVal = icmCityRadarService.getMaxDate("stat_type='week'");
		}else if(StringUtils.isBlank(dateVal)){
			dateVal = icmCityRadarService.getMaxDate("stat_type='day'");
		}
		String fields[] ={"disp","click","acp","cpm2","ctr2"};
		List<String> cityArr = Lists.newArrayList(citys_radar.split(","));
		String condition =  IncomeCityConst.getCityRadarCon(stat_type,dateVal,businessName, cityArr,is_vip);
		List<IncomeCityRadar> list = icmCityRadarService.getListByConditon(null, condition, null);
		TreeMap<String, List<IncomeCityRadar>> stat_dateMap = new TreeMap<String, List<IncomeCityRadar>>(ReflectUtil.groupByField(list,"stat_date"));

		JSONObject ob = new JSONObject();
		JSONArray dataList = new JSONArray(); //用于echarts的 series的多组data
		for (String date : stat_dateMap.keySet()) {
			JSONArray dayCityData = new JSONArray(); //timeline的一天的数据 [ {name:北京,value:[]}, {name:上海,value:[]} ]
			for (IncomeCityRadar cityVo: stat_dateMap.get(date)){
				JSONObject jvo = new JSONObject();
				jvo.put("name", cityVo.getCity_name());
				List<Number> curList =  com.google.common.collect.Lists.newArrayList();
				for (String f : fields) {
					if("disp".equals(f) || "click".equals(f)){
						curList.add((Number) ReflectUtil.getVal(cityVo, f));
					}else{
						curList.add(DNumberUtils.trans2Float((Float) ReflectUtil.getVal(cityVo, f)*10));
					}
				}
				jvo.put("value", curList);
				dayCityData.add(jvo);
			}
			dataList.add(dayCityData);
		}
		ob.put("time", stat_dateMap.keySet());
		ob.put("dataList", dataList);
		
		return ob.toString();

	}

	/**
	 * 城市雷达图对应表格明细
	 */
	public static String getCityRadarTableJson(String stat_type,String dateVal,
			String businessName, String is_vip) {
		String condition =  IncomeCityConst.getCityRadarTableCon(stat_type,dateVal, businessName,is_vip);
		List<IncomeCityRadar> list = icmCityRadarService.getListByConditon(null, condition, "click desc");
		
		JSONObject ob = new JSONObject();
		ob.put("dataList", list);

		return ob.toString();

	}
	
	/**
	 * 类别雷达图
	 */
	public static String getCateRadarJson(String stat_type,String dateVal,
			String businessName, String cates_radar,String is_vip) {
		if(StringUtils.equals(stat_type, "week")){
			dateVal = icmCateRadarService.getMaxDate("stat_type='week'");
		}else if(StringUtils.isBlank(dateVal)){
			dateVal = icmCateRadarService.getMaxDate("stat_type='day'");
		}
		String fields[] ={"disp","click","acp","cpm2","ctr2"};
		List<String> cateArr = Lists.newArrayList(cates_radar.split(","));
		String condition =  IncomeCateConst.getCateRadarCon(stat_type,dateVal,businessName, cateArr,is_vip);
		List<IncomeCateRadar> list = icmCateRadarService.getListByConditon(null, condition, null);
		TreeMap<String, List<IncomeCateRadar>> stat_dateMap = new TreeMap<String, List<IncomeCateRadar>>(ReflectUtil.groupByField(list,"stat_date"));

		JSONObject ob = new JSONObject();
		JSONArray dataList = new JSONArray(); //用于echarts的 series的多组data
		for (String date : stat_dateMap.keySet()) {
			JSONArray dayCateData = new JSONArray(); //timeline的一天的数据 [ {name:北京,value:[]}, {name:上海,value:[]} ]
			for (IncomeCateRadar cityVo: stat_dateMap.get(date)){
				JSONObject jvo = new JSONObject();
				jvo.put("name", cityVo.getCate_name());
				List<Number> curList =  com.google.common.collect.Lists.newArrayList();
				for (String f : fields) {
					if("disp".equals(f) || "click".equals(f)){
						curList.add((Number) ReflectUtil.getVal(cityVo, f));
					}else{
						curList.add(DNumberUtils.trans2Float((Float) ReflectUtil.getVal(cityVo, f)*10));
					}
				}
				jvo.put("value", curList);
				dayCateData.add(jvo);
			}
			dataList.add(dayCateData);
		}
		ob.put("time", stat_dateMap.keySet());
		ob.put("dataList", dataList);
		return ob.toString();

	}

	/**
	 * 类别雷达图对应表格明细
	 */
	public static String getCateRadarTableJson(String stat_type,String dateVal,
			String businessName, String is_vip) {
		String condition =  IncomeCateConst.getCateRadarTableCon(stat_type,dateVal, businessName,is_vip);
		List<IncomeCateRadar> list = icmCateRadarService.getListByConditon(null, condition, "click desc");
		
		JSONObject ob = new JSONObject();
		ob.put("dataList", list);

		return ob.toString();

	}
	
	
	/**
	 * 分平台(精准，置顶，智能)趋势图数据
	 */
	public static String getPlatformJson(String dateGroup, String stat_type,String index, String businessName, String product){
		JSONObject ob = new JSONObject();
		String plats[] ={"PC","M","APP"};
		String cols = IncomePlatConst.getPlatCol(dateGroup,index);
		String condition =  IncomePlatConst.getPlatCon(stat_type, businessName, product);
		List<IncomePlatform> list = icmPlatService.getListByConditon( cols, condition, dateGroup);
		TreeMap<String, List<IncomePlatform>> stat_dateMap = ReflectUtil.groupByField(list,dateGroup);

		HashMap<String, List<String>> dataMap = new HashMap<String, List<String>>(); // stat_date : [dataList]
		for (String date : stat_dateMap.keySet()) {
			for(String p : plats){

				List<String> curList = dataMap.get(p);
				if(curList == null)
					curList = Lists.newArrayList();
				IncomePlatform vo = ReflectUtil.getByColVal(IncomePlatform.class,stat_dateMap.get(date),"platform",p);
				curList.add((String) ReflectUtil.getVal(vo, index));
				dataMap.put(p, curList);
			}
		}
		ob.put("time", stat_dateMap.keySet());
		
		ob.putAll(dataMap);

		return ob.toString();
	}
	
	/**
	 * echarts点击获取城市对应的产品(精准，置顶，智能) 表格展示数据
	 */
	public static String getPlatformTableJson(String dateGroup, String stat_type, String dateVal, String businessName,String product){
		String condition =  IncomePlatConst.getCityProTableCon(dateGroup, stat_type, dateVal, businessName,product);
		List<IncomePlatform> list = icmPlatService.getListByConditon( null, condition, "cost desc");

		JSONObject ob = new JSONObject();
		ob.put("dataList", list);

		return ob.toString();
	}
	
}
